"-----------------------------------------------------------------------------
" Misc configurations
"-----------------------------------------------------------------------------

" Turns off compability with vi. Generally a good thing, plus it should be done
" first seems it resets some options. Up to now I found it does backupcopy=auto.
set nocompatible

" Leader key is space
let mapleader = " "

" Syntax file
set syntax=acme

" Swap and recovery file directories
set backupdir=~/.vim/recovery//
set directory=~/.vim/swap//

" Turn on omnicompletion
set omnifunc=syntaxcomplete#Complete

" Other simple configurations
set tabstop=4
set shiftwidth=4
set tw=80
set smarttab
set autoindent
set relativenumber
set autowrite 

" Folds 
set foldmethod=indent
set nofoldenable

" Better searching
set smartcase
set ignorecase
set incsearch
nnoremap <cr> :noh<cr>

" Completion
inoremap <S-tab> <C-n>

" Show tab characters
set listchars=tab:>-

" Toggle Immersive mode
nnoremap <leader>I :Immersive<CR>

" Compiling
nnoremap <leader>m :make<cr>

" Toggle paste mode
set pastetoggle=<F11>

" Y copy until end of line
nnoremap Y y$

" Safe exit
nnoremap <leader>Q :wa \| :qa<cr>

" Quick global substituttion
nnoremap <leader>* :%s/<c-r><c-w>//g<left><left>

" Remap redo so it doesnt interfere with ratpoison
nnoremap R <C-R>

"-----------------------------------------------------------------------------
" General movement
"-----------------------------------------------------------------------------

" Scrolling
nnoremap <Down> 2<C-E>
nnoremap <Up> 2<C-Y>


" Relative movement
nnoremap + :+
nnoremap - :-
nnoremap go <C-o>

"-----------------------------------------------------------------------------
" Tabs, buffers and splits
"-----------------------------------------------------------------------------

" Tabs
nnoremap <leader>tc :tabnew<space>
nnoremap <leader>tl :tabnext<cr>
nnoremap <leader>th :tabprevious<cr>
nnoremap <leader>tH :tabm -1<cr>
nnoremap <leader>tL :tabm +1<cr>

" Buffer movement
nnoremap <leader>n :wn<CR>
nnoremap <leader>p :wprev<CR>
nnoremap <leader>l <C-w>l
nnoremap <leader>h <C-w>h
nnoremap <leader>k <C-w>k
nnoremap <leader>j <C-w>j

" Splits and windows
nnoremap <leader>C :split<cr>
nnoremap <leader>c :vsplit<cr>
nnoremap <leader>q ZZ
nnoremap <leader>+ :res +5<cr>
nnoremap <leader>- :res -5<cr>
nnoremap <leader>> :vertical-res +10<cr>
nnoremap <leader>< :vertical-res -10<cr>

"-----------------------------------------------------------------------------
" Simple auto-closing symbols
"-----------------------------------------------------------------------------
inoremap " ""<left>
inoremap ' ''<left>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap {<space> {}<left><space><left><space>
inoremap {<cr> {<cr>}<esc>O<tab>
inoremap {;<cr> {<cr>};<esc>O<tab>

"-----------------------------------------------------------------------------
" Make '+' and '-' move lines up and down
"-----------------------------------------------------------------------------
fu! MoveLineUp()
	if line('.') == 1
		return
	endif
	if line('.') == line('$')
		norm! ddP
	else
		norm! ddkP
	endif
endf
nnoremap B :call MoveLineUp()<cr>
nnoremap W ddp

"-----------------------------------------------------------------------------
" Netrw
"-----------------------------------------------------------------------------

" Configurations
let g:netrw_keepdir = 0
let g:netrw_winsize = 25
let g:netrw_banner = 0
let g:netrw_liststyle = 3

" Changes to commands
let g:netrw_localcopydircmd = 'cp -r'
let g:netrw_localrmdir='rm -rf' " Careful with this

nnoremap <leader>d :Lexplore<cr>

"-----------------------------------------------------------------------------
" Easy access to configs
"-----------------------------------------------------------------------------
" Edit and source vimrc
nnoremap <leader>ec :tabedit $MYVIMRC<cr>
nnoremap <leader>S :source $MYVIMRC<cr>

" Edit tmux.confg
nnoremap <leader>et :tabedit ~/.config/tmux/tmux.conf<cr>
